//
//  BLEDevice.h
//  BLESDKDemo
//
//  Created by 李阳 on 2018/4/5.
//  Copyright © 2018年 李阳. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
#import "BLEConstant.h"

/// 设备连接状态
typedef NS_ENUM(NSUInteger, BLEDeviceConnectState)
{
    BLEDeviceStateDisconnected = 0, ///< 已经断开连接
    BLEDeviceStateConnecting, ///< 正在连接
    BLEDeviceStateConnected, ///< 已连接
    BLEDeviceStateDisconnecting, ///< 即将断开连接
};

/// 设备功能状态
typedef NS_OPTIONS(NSUInteger, BLEDeviceReachabilityStatus)
{
    BLEDeviceReachabilityStatusNotReachable = 0, ///< 什么功能都不可以，比如断连
    BLEDeviceReachabilityStatusSession = 1 << 1, ///< 可以发送数据
    BLEDeviceReachabilityStatusMedia = 1 << 2, ///< 可以发送媒体数据
    BLEDeviceReachabilityStatusHeart = 1 << 3, ///< 可以获取心率数据
    BLEDeviceReachabilityStatusNFC = 1 << 4, ///< 可以NFC
    BLEDeviceReachabilityStatusPay = 1 << 5, ///< 可以支付
    BLEDeviceReachabilityStatusOTA = 1 << 6, ///< 可以OTA
};

@class BLENormalTask, BLEMediaTask, BLEContactTask;


/// 蓝牙设备
@interface BLEDevice : NSObject

/// 设备名称
@property (nonatomic, copy, readonly) NSString *name;
/// 设备状态
@property (nonatomic, assign, readonly)BLEDeviceConnectState state;
/// 设备功能状态
@property (nonatomic, assign, readonly) BLEDeviceReachabilityStatus status;
/// 信号强度
@property (nonatomic, strong, readonly)NSNumber *RSSI;
/// 外设
@property (nonatomic, strong, readonly) CBPeripheral *peripheral;
/// 是从已连接设备中取回或者是扫描得到
@property (nonatomic, assign, readonly) BOOL retrieveFromCenter;

#pragma mark Prepare
- (void)prepareForSessionWithTimeout:(NSTimeInterval)timeout
                       completion:(void(^)(NSTimeInterval use, NSError *error))completion;

- (void)prepareForNFCWithTimeout:(NSTimeInterval)timeout
                          completion:(void(^)(NSTimeInterval use, NSError *error))completion;

- (void)prepareForPayWithTimeout:(NSTimeInterval)timeout
                          completion:(void(^)(NSTimeInterval use, NSError *error))completion;

- (void)prepareForOTAWithTimeout:(NSTimeInterval)timeout
                          completion:(void(^)(NSTimeInterval use, NSError *error))completion;

#pragma mark Request

- (void)subscribeWithCmd:(Byte)cmd
       advertisedChannel:(BLEAdvertisedChannel)channel
                    next:(void(^)(NSData *responseData, Byte cmd))block;

- (void)beginNormalTask:(BLENormalTask *)task;
- (void)beginMediaTask:(BLEMediaTask *)task;
- (void)beginContactTask:(BLEContactTask *)task;


#pragma mark ReadRSSI
- (void)readRSSIWithTimeout:(NSTimeInterval)timeout
                 completion:(void(^)(NSTimeInterval use, NSNumber *RSSI, NSError *error))completion;


#pragma mark NSObject
+ (instancetype)deviceWithPeripheral:(CBPeripheral *)peripheral
                                RSSI:(NSNumber *)rssi
                   advertisementData:(NSDictionary<NSString *,id> *)advertisementData;

- (instancetype)initWithPeripheral:(CBPeripheral *)peripheral
                              RSSI:(NSNumber *)rssi
                 advertisementData:(NSDictionary<NSString *,id> *)advertisementData  NS_DESIGNATED_INITIALIZER;

- (NSComparisonResult)compareDevice:(BLEDevice *)device;

- (BOOL)isEqualToDevice:(BLEDevice *)device;

- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;

@end
